package com.example.demo.controller;

import com.example.demo.common.domain.*;
import com.example.demo.common.util.Result;
import com.example.demo.service.ComLeaveService;
import com.example.demo.service.LeaveService;
import com.example.demo.service.OutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/general")
public class GeneralController {
    @Autowired
    @Qualifier("leaveServiceImpl")
    LeaveService leaveService;
    @Autowired
    OutService outService;
    public boolean check(HttpSession session){
        GeneralManager generalManager = (GeneralManager)session.getAttribute("generalManger");
        if(generalManager != null){
            return true;
        }
        return false;
    }

    @Autowired
    ComLeaveService comLeaveService;
    @RequestMapping("/agreeLeaveItem")
    @ResponseBody//根据传入的请假号，更新该请假表单中的字段 此处为总经理ID和总经理同意
    public Map agreeLeaveItem(HttpSession session, Integer lid){

        GeneralManager g = (GeneralManager)session.getAttribute("generalManger");
        leaveService.updateGIdByPrimaryId(lid,g.getGid());
        return Result.success(true);
    }

    @RequestMapping("/agreeOutItem")
    @ResponseBody//根据传入的外出号，更新该外出表单中的字段 此处为总经理ID和总经理同意
    public Map agreeOutItem(HttpSession session, Integer oid){

        GeneralManager g = (GeneralManager)session.getAttribute("generalManger");
        outService.updateGIdByPrimaryId(oid,g.getGid());
        return Result.success(true);
    }
    @RequestMapping("/agreeComLeaveItem")
    @ResponseBody//根据传入的调休号，更新该请假表单中的字段 此处为总经理ID和总经理同意
    public Map agreeComLeaveItem(HttpSession session, Integer cid){

        GeneralManager g = (GeneralManager)session.getAttribute("generalManger");
        comLeaveService.updateGIdByPrimaryId(cid,g.getGid());
        return Result.success(true);
    }
    @RequestMapping("/agreeleave")//搜索副总经理通过的请假申请表单，跳转到总经理经理同意请假申请页面
    public String agreeleave(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                             @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        if(!check(session)){
            GeneralManager generalManager = new GeneralManager();
            generalManager.setGid(1);
            generalManager.setGname("王经理");
            session.setAttribute("generalManger",generalManager);
        }


        List<LeaveRecordPlus> leave_records = leaveService.GeneralSelectAllLeave();//连接查询 请假表和员工信息表一起查询
        model.addAttribute("list",leave_records);
        return "manger/general/leavelist";
    }
    @RequestMapping("/agreecomleave")//搜索副总经理通过的调休申请表单，跳转到总经理经理同意调休申请页面
    public String agreecomleave(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        if(!check(session)){
            GeneralManager generalManager = new GeneralManager();
            generalManager.setGid(1);
            generalManager.setGname("王经理");
            session.setAttribute("generalManger",generalManager);
        }


        List<CompensatoryLeavePlus> compensatoryLeavePluses = comLeaveService.selectAllPlus();
        model.addAttribute("list",compensatoryLeavePluses);
        return "manger/general/comleavelist";
    }
    @RequestMapping("/agreeout")//搜索副总经理通过的外出申请表单，跳转到总经理经理同意外出申请页面
    public String agreeout(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                           @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        if(!check(session)){
            GeneralManager generalManager = new GeneralManager();
            generalManager.setGid(1);
            generalManager.setGname("王经理");
            session.setAttribute("generalManger",generalManager);
        }


        List<OutRecordPlus> recordPluses = outService.GeneralSelectAllOut();//连接查询 请假表和员工信息表一起查询
        System.out.println(recordPluses.size());
        model.addAttribute("list",recordPluses);
        return "manger/general/outlist";
    }
}
